/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my.algorithms.mcorrea;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 *
 * @author mcorrea
 */
public class String1Test {
	
	public static void main(String args[]){
	
		orderNonrepetingChar("geeksforgeeks");	
		
	
	}
	
	
	
	
	public static void orderNonrepetingChar(String str){
	
		Map<Character, Integer> counts = new HashMap<Character, Integer>();
		
		for(Character c: str.toCharArray() ){
			if(counts.containsKey(c)){
				counts.put(c, counts.get(c)+1);	
			}else{
				counts.put(c, 1);
			}
		}
		
		Iterator ite = counts.entrySet().iterator();
		while(ite.hasNext()){
			Map.Entry<Character, Integer> entry = (Map.Entry<Character, Integer>)ite.next(); 
			System.out.println(entry.getKey()+": "+entry.getValue());
		}
		
		System.out.println("--------------");
		
		
		
		TreeMap<Character, Integer> smap = new TreeMap(new ValueComparator(counts));
		
		smap.putAll(counts);
		
		ite = smap.entrySet().iterator();
		while(ite.hasNext()){
			Map.Entry<Character, Integer> entry = (Map.Entry<Character, Integer>)ite.next(); 
			System.out.println(entry.getKey()+": "+entry.getValue());
		}
		
	
	}
	
	
	
	private static class ValueComparator implements Comparator {

		  Map base;
		  public ValueComparator(Map base) {
			  this.base = base;
		  }

		  public int compare(Object a, Object b) {

			  return ((Integer)base.get(a)).compareTo((Integer)base.get(b) );
			  
		  }
	}
	
	
	
	public static void checkString(){
		String A = "abcdabc";
			String B = "efgabcdabc";
			
			
			List<Character> listB = new ArrayList<Character>();
			for(int c=0;c<B.length();c++){
				listB.add(new Character(B.charAt(c)));
			}
			Collections.sort(listB);
			
			
			boolean all = true;
			for(int a=0;a<A.length();a++){
				System.out.println(Collections.binarySearch(listB, new Character(A.charAt(a))) );
				if(Collections.binarySearch(listB, new Character(A.charAt(a)) ) <0){
					all = false;
					break;
					
				}
			}
			
			if(all)System.out.println("All chars in A are in B");
			else System.out.println("Not all character in A are in B");
	}
	
	
	
	
}
